var express = require('express');
var router = express.Router();
var Comment = require('../models/comment.js');
var Ip = require('../models/ip.js');
var Post = require('../models/posts.js');
var getTime = require('../models/date.js');
router.post('/',function(req,res,next){

    var mailReg = new RegExp(/^[^\._-][\w\.-]+@([A-Za-z0-9]+\.)+[A-Za-z]+$/);
    if(!mailReg.test(req.body.mail)){
        res.json({"success":false,message:'邮箱格式不正确',prompt:"mailErr"});
    }
    var newComment = req.body;
    var nowTime = getTime();
    var ipOrigin = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
    var ip = ipOrigin.split(':')[3];
    console.log(ip);
    var comment = new Comment(newComment);

    Ip.getOne(ip,function(err,ipresult){
        console.log(ipresult);
        if(err){
            console.log(err);
            res.json({"success":false,message:'添加评论失败',prompt:""});
            return 0;
        }else if(!ipresult){
            console.log('err1');
            var ipobj = new Ip({ip:ip,count:0});
            ipobj.save(function(err){
                if(err){
                    console.log('err11');
                    res.json({"success":false,message:'添加评论失败',prompt:""});
                    return 0;
                }
            });
            comment.save(function(err){
                console.log('err5');
                if(err){
                    console.log('err66');
                    console.log(err);
                    res.json({"success":false,message:'添加评论失败',prompt:""});
                    return false;
                }else{
                    res.json({"success":true,message:'添加评论成功',prompt:""});
                }
            });
        }else{
            console.log('err2');
            var lastTime = new Date(ipresult.lastTime);
            console.log(nowTime['date'] - lastTime);
            if(nowTime['date'] - lastTime<10000){
                console.log('time-fail');
                console.log(getTime()['second']);
                Ip.update({ip:ip},{lastTime:getTime()['second']},function(err){
                    if(err){
                        console.log('err3');
                        res.json({"success":false,message:'添加评论失败',prompt:""});
                        return 0;
                    }
                });
                res.json({"success":false,message:'添加评论失败',prompt:"last-firstTime too small"});
                return 0;
            }else{
                console.log('time-success');
                console.log(nowTime['second']);
                console.log(ip);
                Ip.update(ip,{count:ipresult.count+1,lastTime:nowTime['second']},function(err,state){
                    console.log('err43');
                    if(err){
                        console.log('err33');
                        res.json({"success":false,message:'添加评论失败',prompt:""});
                        return 0;
                    }
                });
            }

            console.log('err4');
            comment.save(function(err){
                console.log('err5');
                if(err){
                    console.log('err6');
                    console.log(err);
                    res.json({"success":false,message:'添加评论失败',prompt:""});
                    return false;
                }else{
                    res.json({"success":true,message:'添加评论成功',prompt:""});
                }
            });
        }
    });


});
module.exports = router;